import {
  Mesh,
  Group,
  BoxGeometry,
  MeshBasicMaterial,
  Vector3,
  ExtrudeGeometry,
  EdgesGeometry,
  LineBasicMaterial,
  LineSegments,
  BufferGeometry,
  FrontSide,
  DoubleSide,
  Material,
} from "three";
import { Base3DGroup } from "./groups";
import { BaseModel , PartModel, LoftModel} from "../Models";
import { KujialeParamModel } from "../KujialeJsonModels/KujialeParamModel";

class BaseMesh extends Mesh{
  model: BaseModel;
  constructor(m: BaseModel){
    super();
    this.model = m;
  }
}

export class Part3DMesh extends BaseMesh {
  constructor(m: PartModel){
    super(m);
  }

  rotateAndMoveByPartModel() {
    var [part] = this.model.kujialeModel!;
    var rx = part.rotate.x;
    var ry = part.rotate.y;
    var rz = part.rotate.z;
    this.rotateOnWorldAxis(new Vector3(1, 0, 0), rx);
    this.rotateOnWorldAxis(new Vector3(0, 0, -1), ry);
    this.rotateOnWorldAxis(new Vector3(0, 1, 0), rz);

    //定位点不是1-8的角点，而是板件的中心位置，所以要做位移
    var rotatedVec = new Vector3(0, part.thickness! / 2, 0);
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(1, 0, 0), rx);
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 0, -1), ry);
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 1, 0), rz);
    this.position.set(
      part.position.x - rotatedVec.x,
      part.position.z - rotatedVec.y,
      -part.position.y - rotatedVec.z
    );
  }
}

export class Loft3DMesh extends BaseMesh {
  constructor(m: LoftModel){
    super(m);
  }

  rotateAndMoveBy3D() {
    var [part] = this.model.kujialeModel!;
    var rx = part.rotate.x;
    var ry = part.rotate.y;
    var rz = part.rotate.z;
    this.rotateOnWorldAxis(new Vector3(1, 0, 0), rx);
    this.rotateOnWorldAxis(new Vector3(0, 0, -1), ry);
    this.rotateOnWorldAxis(new Vector3(0, 1, 0), rz);

    //定位点不是1-8的角点，而是板件的中心位置，所以要做位移
    var rotatedVec = new Vector3(
      -part.size!["x"] / 2,
      -part.size!["z"] / 2,
      part.size!["y"] / 2
    );
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(1, 0, 0), rx);
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 0, -1), ry);
    rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 1, 0), rz);
    this.position.set(
      this.position.x + part.position.x - rotatedVec.x,
      this.position.y + part.position.z - rotatedVec.y,
      this.position.z - part.position.y - rotatedVec.z
    ); //box是mesh，所以用xyz顺序，part是json模型，遵照的是右手坐标系，所以得换成xz-y顺序
  }

  rotateAndMoveByLoftModel() {
    var [part] = this.model.kujialeModel!;
    var rx = part.rotate.x;
    var ry = part.rotate.y;
    var rz = part.rotate.z;
    this.rotateOnWorldAxis(new Vector3(1, 0, 0), rx);
    this.rotateOnWorldAxis(new Vector3(0, 0, -1), ry);
    this.rotateOnWorldAxis(new Vector3(0, 1, 0), rz);


    this.position.set(
      this.position.x + part.position.x ,
      this.position.y + part.position.z ,
      this.position.z - part.position.y 
    ); //box是mesh，所以用xyz顺序，part是json模型，遵照的是右手坐标系，所以得换成xz-y顺序
  }
}

export class Box3DMesh extends BaseMesh {
  constructor(m: BaseModel){
    super(m);
  }

  rotateAndMoveByModel() {
    var [part] = this.model.kujialeModel!;
    var rx = part.rotate.x;
    var ry = part.rotate.y;
    var rz = part.rotate.z;
    this.rotateOnWorldAxis(new Vector3(1, 0, 0), rx);
    this.rotateOnWorldAxis(new Vector3(0, 0, -1), ry);
    this.rotateOnWorldAxis(new Vector3(0, 1, 0), rz);

    this.position.set(
      this.position.x + part.position.x ,
      this.position.y + part.position.z ,
      this.position.z - part.position.y 
    ); 

    // //定位点不是1-8的角点，而是板件的中心位置，所以要做位移
    // var rotatedVec = new Vector3(
    //   -part.size!["x"] / 2,
    //   -part.size!["z"] / 2,
    //   part.size!["y"] / 2
    // );
    // rotatedVec = rotatedVec.applyAxisAngle(new Vector3(1, 0, 0), rx);
    // rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 0, -1), ry);
    // rotatedVec = rotatedVec.applyAxisAngle(new Vector3(0, 1, 0), rz);
    // this.position.set(
    //   this.position.x + part.position.x - rotatedVec.x,
    //   this.position.y + part.position.z - rotatedVec.y,
    //   this.position.z - part.position.y - rotatedVec.z
    // ); //box是mesh，所以用xyz顺序，part是json模型，遵照的是右手坐标系，所以得换成xz-y顺序
  }
}

export class MouseSenseCube extends Mesh {
  mytype: string;
  constructor(geometry: BoxGeometry, material: Material) {
    super(geometry, material);
    this.mytype = "SenseCube";
  }
}
